package com.mall.web.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**
 * 获取到当前登录是来源于哪里。 来源IOS ANDROID H5
 * 
 * @author youbush
 *
 */
public class APPCookieUtil {

	public static String getCookie(HttpServletRequest request) {
		Cookie cookies[] = request.getCookies();
		if (cookies == null) {
			return null;
		}
		Cookie sCookie = null;
		String svalue = null;
		String sname = null;
		for (int i = 0; i < cookies.length; i++) {
			sCookie = cookies[i];
			sname = sCookie.getName();
			svalue = sCookie.getValue();
			if (sname.equals("fromApp")) {
				return svalue;
			}
		}
		return null;
	}

	public static int urlFromApp(HttpServletRequest request) {
		String fromApp = getCookie(request);
		if (StringUtils.isNotBlank(fromApp)) {
			if (fromApp.equalsIgnoreCase("ios")) {
				return 1;
			}
			if (fromApp.equalsIgnoreCase("android")) {
				return 1;
			}
		}
		fromApp = request.getParameter("fromApp");
		if (StringUtils.isNotBlank(fromApp)) {
			if (fromApp.equalsIgnoreCase("ios")) {
				return 1;
			}
			if (fromApp.equalsIgnoreCase("android")) {
				return 1;
			}
		}
		return 0;
	}
}
